<template>
  <div>
    <el-table
      :data="permissionArr"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :expand-row-keys="expandKeys"
    >
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column label="权限值" prop="code"></el-table-column>
      <el-table-column label="修改时间" prop="updateTime"></el-table-column>
      <el-table-column label="操作">
        <!--      <template #="{ row, $index }">-->
        <template #="{ row }">
          <el-button
            type="primary"
            :icon="Plus"
            @click="addPermission(row)"
            :size="row.level == 2 || row.level == 1 ? 'default' : 'small'"
            :disabled="row.level == 4"
          >
            {{ row.level == 3 ? '功能' : '菜单' }}
          </el-button>
          <el-button
            type="primary"
            :icon="Edit"
            @click="updatePermission(row)"
            :size="row.level == 2 || row.level == 1 ? 'default' : 'small'"
            :disabled="row.level == 1"
          ></el-button>

          <el-popconfirm
            :title="`Are you sure to delete the ${row.name}?`"
            width="260px"
            @confirm="removeMenu(row.id)"
          >
            <template #reference>
              <el-button
                type="danger"
                :size="row.level == 2 || row.level == 1 ? 'default' : 'small'"
                :disabled="row.level == 1"
              >
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--  对话框-->
    <el-dialog
      v-model="dialogVisible"
      :title="menuData.id ? '更新菜单' : '添加菜单'"
    >
      <!--    表单组件-->
      <el-form>
        <el-form-item label="名称">
          <el-input
            placeholder="请输入菜单名称"
            v-model="menuData.name"
          ></el-input>
        </el-form-item>
        <el-form-item label="权限">
          <el-input
            placeholder="请输入权限数值"
            v-model="menuData.code"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">Cancel</el-button>
          <el-button type="primary" @click="confirm">Confirm</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import {
  reqAddOrUpdateMenu,
  reqAllPermission,
  reqRemoveMenu,
} from '@/api/acl/menu'
import {
  MenuParams,
  Permission,
  PermissionList,
  PermissionResponseData,
} from '@/api/acl/menu/type.ts'
import { Edit, Plus } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

onMounted(() => {
  getHasPermission()
})
// 存储菜单权限列表
let permissionArr = ref<PermissionList>([])
/**
 * 获取菜单权限列表
 */
const getHasPermission = async () => {
  const result: PermissionResponseData = await reqAllPermission()
  if (result.code == 200) {
    permissionArr.value = result.data
  }
}
// 控制对话框显示与隐藏
let dialogVisible = ref(false)
// 收集上传需要的参数
let menuData = reactive<MenuParams>({
  code: '',
  level: 0,
  name: '',
  pid: 0,
})
// 添加菜单权限按钮
const addPermission = (row: Permission) => {
  Object.assign(menuData, {
    code: '',
    level: 0,
    name: '',
    pid: 0,
  })
  dialogVisible.value = true
  // 收集参数
  menuData.level = row.level + 1 // row是当前行，需要新增的是当前行的子菜单，所以需要+1
  // 给哪个菜单新增子菜单，那么新增的菜单的父级id就是当前行的id
  menuData.pid = row.id as number
}
// 修改菜单权限按钮
const updatePermission = (row: Permission) => {
  dialogVisible.value = true
  // 收集数据, 修改权限就是这行数据，所以直接赋值 menuData 即可获得pid leve id  旧name 旧code
  Object.assign(menuData, row)
  // name 和 code 由对话框双向绑定获得
}
// 对话框确定按钮
const confirm = async () => {
  // 发送请求
  let result: any = await reqAddOrUpdateMenu(menuData)
  if (result.code == 200) {
    dialogVisible.value = false
    ElMessage.success(menuData.id ? '修改成功' : '添加成功')
    await getHasPermission()
  }
}
// 需要展开的行的id
const expandKeys = ref<string[]>(['1'])
const removeMenu = async (id: number) => {
  const result: any = await reqRemoveMenu(id)
  if (result.code == 200) {
    ElMessage.success('删除成功')
    await getHasPermission()
  }
}
</script>

<style scoped lang="scss"></style>
